package com.wframe.project.former.dataservices.faretemp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;

import com.frame.database.JBaseDao;
import com.frame.database.JdataBean;

public class FaretempDao extends JBaseDao{

	public FaretempDao() {
		super("wb_faretemp");
	}
	public List<JdataBean> queryTemp() throws Exception{
		return getSession().query("select * from wb_faretemp order by id", null);
	}
	
	public List<JdataBean> queryAreas(String tempid) throws Exception{
		return getSession().query("select * from wb_faretemp_area where tempid=? order by id", new Object[]{tempid});
	}
	
	public void saveDefault(JdataBean bean) throws Exception{
		bean.put("name", "默认运费");
		bean.put("type", 0);
		JdataBean get=getDefault();
		if(get==null){
			getSession().save("wb_faretemp",bean.getDataMap());
		}else{
			Map key=new HashMap();
			key.put("id", get.getString("id"));
			getSession().update("wb_faretemp",bean.getDataMap(),key);
		}
	}
	public JdataBean getDefault() throws Exception{
		return getSession().getObject("select * from wb_faretemp where type=0", null);
	}
	public String saveTemp(JdataBean bean) throws Exception{
		if(StringUtils.isNotBlank(bean.getString("id"))){
			Map key=new HashMap();
			key.put("id", bean.getString("id"));
			getSession().update("wb_faretemp",bean.getDataMap(),key);
			return bean.getString("id");
		}else{
			return getSession().saveReturnKey("wb_faretemp",bean.getDataMap());
		}
	}
	public void saveArea(String tempid,List<JdataBean> areas) throws Exception{
		delAreas(tempid);
		for(JdataBean area:areas){
			area.put("tempid", tempid);
			getSession().saveReturnKey("wb_faretemp_area",area.getDataMap());
		}
	}
	
	public void delAreas(String tempid) throws HibernateException, Exception{
		getSession().delete("delete from wb_faretemp_area where tempid=?",new Object[]{tempid});
	}
}
